package edu.stthomas.seis770.visitor;

import COM.hilbertinc.xml.HDOMUtil;
import COM.hilbertinc.xml.HDumperVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import java.io.Writer;

/**
 * HTMLVisitor class to address exercise part2
 */
public class HTMLVisitor extends HDumperVisitor {

    @Override
    public void processDocumentProlog(Document document) throws Exception {
        Writer html = getWriter();
        html.write("<html>\n");
        indentMore();
        html.write(getIndent());
        html.write("<body>\n");
    }

    @Override
    public void processDocumentType(DocumentType dtd) throws Exception {
        Writer html = getWriter();
        indentMore();
        html.write(getIndent());
        html.write("<h1>");
        html.write("!DOCTYPE ");
        html.write(dtd.getName());
        html.write(" SYSTEM ...");
        html.write("</h1>\n");
        indentLess();
    }

    @Override
    public void processDocumentEpilog(Document document) throws Exception {
        Writer html = getWriter();
        indentLess();
        html.write(getIndent());
        html.write("</body>\n");
        indentLess();
        html.write(getIndent());
        html.write("</html>\n");
    }

    @Override
    public void processElementProlog(Element node) throws Exception {
        Writer html = getWriter();
        indentMore();
        html.write(getIndent());
        html.write("<h3>");
        html.write(node.getTagName());
        html.write("</h3>\n");
    }

    @Override
    public void processElementEpilog(Element node) throws Exception {
        Writer html = getWriter();
        indentLess();
        html.write(getIndent());
        html.write("<h3>");
        html.write(node.getNodeName());
        html.write("</h3>\n");
    }

    @Override
    public void processText(Text text) throws Exception {
        String textString = HDOMUtil.trim(text);
        if (textString.equals("") || textString.equals("\\n")) {
            return;
        }
        Writer html = getWriter();
        indentMore();
        html.write(getIndent());
        html.write("<h2>");
        html.write(textString);
        html.write("</h2>\n");
    }

}
